package com.hungry.hfw.dao;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.hungry.hfw.dao.Annotations.Entity;
import com.hungry.hfw.dao.Annotations.PrimaryKey;
import com.hungry.hfw.dao.Annotations.Volatile;
import com.hungry.web.test.User;



public class ReflectionHolder {
	public final static String INSERT 	= 	"insert into ";
	public final static String SERIALVERSIONUID = "serialVersionUID";
	private String AutoGenKey = null;
	private List<Object> valueList = new ArrayList<Object>();
	private Object[] valueArray;
	public List<Object> getValueList() {
		return valueList;
	}


	public void setValueList(List<Object> valueList) {
		this.valueList = valueList;
	}


	private String abbreviation = "a2";
	public StringBuilder getCompleteEntity(Object object) throws IllegalArgumentException, IllegalAccessException {
		StringBuilder sb = new StringBuilder();
		Class<?> cls = object.getClass();
		Entity e = cls.getAnnotation(Entity.class);
		if(e!=null) {
			System.out.println("abbreviation got - " + e.abbreviation());
			setAbbreviation(e.abbreviation());
		}
		else {
			System.out.println("NULL");
		}
		sb.append(INSERT);
		sb.append(e.tableName());
		sb.append(" (");
		Field[] fields = cls.getDeclaredFields();
		
		boolean start = true;
		int numberOfColumns = 0;
		for(Field f : fields) {
			boolean access = f.isAccessible();
			f.setAccessible(true);
			Annotation[] ans = f.getDeclaredAnnotations();
			boolean isVolatile = false;
			boolean isAutoGenerated = false;
			for(Annotation a : ans) {
				System.out.println(a.getClass());
				
				if(a instanceof Volatile) {
					isVolatile = true;
					System.out.println("inside volatile");
					break;
				}
				if(a instanceof PrimaryKey) { 
					isAutoGenerated = true;
					AutoGenKey = f.getName();
					System.out.println(f.getName() + " - primary key");
				}
			}
			if(isVolatile || isAutoGenerated) {
				continue;
			}
			if(f.getName().contains(SERIALVERSIONUID) ){
				continue;
			}
			else {
				if(!start) {
					sb.append(",");
				}
				sb.append(f.getName());
				valueList.add(f.get(object));
				numberOfColumns++;
				if(start) {
					start = false;
				}
			}
			f.setAccessible(access);
		}
		sb.append(" ) Values (");
		start = true;
		for(int i=0; i <numberOfColumns ; i++) {
			if(!start) {
				sb.append(",");
				start = false;
			}
			sb.append("?");
			if(start) {
				start = false;
			}

		}
		sb.append(")");
			return sb;
		}
		
	
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
	Class<?>[] classes = {User.class};
	System.out.println("class size  - " + classes.length);
	User s = new User();
	System.out.println(new ReflectionHolder().getCompleteEntity(s));

}


public void setAbbreviation(String abbreviation) {
	this.abbreviation = abbreviation;
}


public String getAbbreviation() {
	return abbreviation;
}




public Object[] getValueArray() {
	valueArray = new Object[getValueList().size()];
	int i = 0;
	for(Object o : valueList) {
		System.out.println(o);
		valueArray[i++] = o;
	}
	return valueArray;
}


public String getAutoGenKey() {
	return AutoGenKey;
}
}

